package com.xuerhua.yxt.controller.sys;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.xuerhua.yxt.common.RspResult;
import com.xuerhua.yxt.common.enums.Status;
import com.xuerhua.yxt.controller.BaseController;
import com.xuerhua.yxt.model.ClassTeachers;
import com.xuerhua.yxt.model.Grid;
import com.xuerhua.yxt.model.TeacherInfo;
import com.xuerhua.yxt.page.Page;
import com.xuerhua.yxt.reqVo.BasePageQueryVO;
import com.xuerhua.yxt.service.IClassTeachersService;
import com.xuerhua.yxt.service.ITeacherService;
import com.xuerhua.yxt.utils.StringUtil;

@RequestMapping("sys/classTeacher")
@Controller
public class SysClassTeacherController extends BaseController {
	
	@Autowired
	private IClassTeachersService classTeachersService;
	@Autowired
	private ITeacherService teacherService;
	
	@RequestMapping(value={"","/"})
	public String Index() {
		return "sys/classTeacher";
	}
	
	@RequestMapping("pageQuery")
	@ResponseBody Object pageQuery(BasePageQueryVO vo){
		try {
			vo.setFinishYear(getCurrentTerm().getFinishYear());
			Page<ClassTeachers> page = new Page<>();
			page.setPageNo(vo.getPage());
			page.setPageSize(vo.getRows());
			page.setParams(vo);
			List<ClassTeachers> list = classTeachersService.selectListByPage(page);
			Grid grid = new Grid();
			grid.setRows(list);
			grid.setTotal(page.getTotalRecord());
			return grid;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	@RequestMapping("addUpdate")
	@ResponseBody Object addUpdate(ClassTeachers record) {
		try {
			List<String> ids = new ArrayList<>();
			if(record.getClassTeacherId() != null) {
				ids.add(String.valueOf(record.getClassTeacherId()));
			}
			if(record.getChineseTeacherId() != null) {
				ids.add(String.valueOf(record.getChineseTeacherId()));
			}
			if(record.getMathTeacherId() != null) {
				ids.add(String.valueOf(record.getMathTeacherId()));
			}
			if(record.getEnglishTeacherId() != null) {
				ids.add(String.valueOf(record.getEnglishTeacherId()));
			}
			if(record.getDeputyClassTeacherId() != null) {
				ids.add(String.valueOf(record.getDeputyClassTeacherId()));
			}
			if(record.getLifeTeacherId() != null) {
				ids.add(String.valueOf(record.getLifeTeacherId()));
			}
			List<TeacherInfo> list = teacherService.selectListByIds(ids);
			if(!StringUtil.isNullOrEmpty(list)) {
				for(TeacherInfo teacher : list) {
					if(record.getClassTeacherId() != null && 
							teacher.getId().intValue() == record.getClassTeacherId().intValue()) {
						record.setClassTeacher(teacher.getName());
					}
					if(record.getChineseTeacherId() != null && 
							teacher.getId().intValue() == record.getChineseTeacherId().intValue()) {
						record.setChineseTeacher(teacher.getName());
					}
					if(record.getMathTeacherId() != null && 
							teacher.getId().intValue() == record.getMathTeacherId().intValue()) {
						record.setMathTeacher(teacher.getName());
					}
					if(record.getEnglishTeacherId() != null && 
							teacher.getId().intValue() == record.getEnglishTeacherId().intValue()) {
						record.setEnglishTeacher(teacher.getName());
					}
					if(record.getDeputyClassTeacherId() != null && 
							teacher.getId().intValue() == record.getDeputyClassTeacherId().intValue()) {
						record.setDeputyClassTeacher(teacher.getName());
					}
					if(record.getLifeTeacherId() != null && 
							teacher.getId().intValue() == record.getLifeTeacherId().intValue()) {
						record.setLifeTeacher(teacher.getName());
					}
				}
			}
			if(record.getId() == null) {
				classTeachersService.insertSelective(record);
			}else {
				classTeachersService.updateByPrimaryKeySelective(record);
			}
			return new RspResult(Status.SUCCESS);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return new RspResult(Status.FAIL);
	}
	
	@RequestMapping("del")
	@ResponseBody Object del(String delIds) {
		try {
			classTeachersService.delByPrimaryKey(
					Arrays.asList(delIds.split(",")));
			return new RspResult(Status.SUCCESS);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return new RspResult(Status.FAIL);
	}
}
